package ljl.alg.wangzheng_camp.round1.twopointers;

public class _05_03_reverse_bit {
    
    /**
     * 改了 1000 次以后改对了
     * 不是特别的好，也没考察到任何编程素养
     * */
    static class not_good {
        String toBinaryString(int i) {
            if (i >= 0) return Integer.toString(i, 2);
            StringBuilder sb = new StringBuilder(32);
            while (i != 0) {
                sb.append(i & 1);
                i >>>= 1;
            }
            return sb.toString();
        }
    
        public int reverseBits(int num) {
            String s = toBinaryString(num);
            String[] split = s.split("0");
        
            if (split.length == 1) {
                return Math.min(split[0].length() + 1, 32);
            }
        
            int max = 0;
            for (int i = 1; i < split.length; i++) {
                int t;
                if ((t = split[i - 1].length() + split[i].length()) > max) {
                    max = t;
                }
            }
            return max + 1;
        }
    }
    
    /**
     * 这个方法过于巧妙，我是想不到
     * */
    static class dp {
        public int reverseBits(int num) {
            int cur = 0, insert = 0, res = 1;
            for (int i = 0; i < 32; i++) {
                if ((num & 1 << i) != 0) {
                    cur++;
                    insert++;
                } else {
                    insert = cur + 1;
                    cur = 0;
                }
                res = Math.max(res, insert);
            }
            return res;
        }
    }
    
    static class traverse {
        public int reverseBits(int num) {
            int cnt = 0, lastZero = -1;
            int res = 0;
            for (int i = 0; i < 33; i++) {
                if ((num & 1) != 0) cnt++;
                else {
                    res = Math.max(res, cnt);
                    cnt = i - lastZero;
                    lastZero = i;
                }
                num >>>= 1;
            }
            return res;
        }
    }
    
}
